<?php

/**
 * Quản lý thông tin sản phẩm cửa hàng
 *
 */
class Storemanagement_ProductController extends Zend_Controller_Action
{
    public function init()
    {
        $this->params = $this->getRequest()->getParams();
        System_Template::using("getit","Admin");
		//System_Template::using("blank","Admin");
        $this->model = new Storemanagement_Model_Product();
        parent::init();
    }
	
    public function indexAction()
    {
		$this->view->maintitle = "Danh sách cửa hàng";
		$model = new Storemanagement_Model_Product();
		
		$id = $this->_request->getParam("id");
		$name = $this->_request->getParam("name");
		$parentid = $this->_request->getParam("parentid"); 
		$status = $this->_request->getParam("status");
		
		$this->view->id = $id;
		$this->view->name = $name;
		$this->view->parentid = $parentid;
		$this->view->status = $status;
		$parameter = array('id' => $id, 'name' => $name, 'parentid' => $parentid,
				'status' => $status);
		
		$count = $model->count($parameter);
		$paging = common::pagingAuto($count);
		$data = $model->SelectAll($parameter, $paging[0], $paging[1]);
		$this->view->data = $data;
		//Lấy danh sách store category ở đây để điền vào Combobox		
		$this->storectgmodel = new Globalconfig_Model_Storecategory();	
		$this->view->listStoreCategory = $this->storectgmodel->SelectAllNoPaging();
		// common::showButton('delete');
		// common::showButton('add');
    }
	
	public function inputAction() {
		// common::showButton('save');
		// common::showButton('back');
		$this->model = new Storemanagement_Model_Product();	
		//khai bao danh muc tinh thanh, danh muc quan huyen o day..
		$this->cityModel = new City_Model_Danhmuctinhthanh();
		
		$this->storeCategoryModel = new Globalconfig_Model_Storecategory();
		$id = $this->_getParam("id");
		$storecategoryid = $this->_getParam("storecategoryid");		
		$this->view->flag = 0;
		$this->view->listTinhThanh = $this->cityModel->SelectAllCity();
		
		if ($id == 0) {
			$this->view->maintitle = "Thêm cửa hàng vào danh sách";
			//Lấy danh sách store category ở đây để điền vào Combobox
			$this->view->listStoreCategory = $this->storeCategoryModel->SelectAllNoPaging();
		} else {
			$this->view->maintitle = "Cập nhật thông tin cửa hàng";
			$data = $this->model->FindById($id);
			$this->view->data = $data;
			//Lấy danh sách store category ở đây để điền vào Combobox
			$this->view->listStoreCategory = $this->storeCategoryModel->SelectAllNoPagingById($storecategoryid);
			//Dien thong tin tinh thanh quan huyen o day.
		}
		$this->view->id = $id;
	}
	
	public function saveAction() {
		$uploader = new System_Upload();
		$uploadInfo = $uploader->upload();
		$id = $this->_getParam("id");
		$name = $this->_request->getParam("name");
		$parentid = $this->_request->getParam("parentid"); 
		if($parentid == '')
			$parentid = 0;
		$status = $this->_request->getParam("status");
		 
		$param = array('storeid' => $id, 'name' => $name, 'parentid' => $parentid,
				'status' => $status);
		$this->model->CheckExitsID($id);
		$this->model->AddNewStoreCategory($param);			
		
		$this->_redirect('/storemanagement/store/index');
		
		}
	
	public function deleteAction() {
		$params = $this->getRequest()->getParams();
		$db = Zend_Db_Table::getDefaultAdapter();
		$ids = $params["DEL"];
		for ($i = 0; $i < count($ids); $i++) {
			$this->model->DeleteStoreCategory($ids[$i]);
		}
		$this->_redirect("/storemanagement/product/index");
	}
	
	public function selectdistrictbycityidAction() {
		$cityid = $this->_request->getParam("cityid");
		//$districtid = 1;
		$districtid = $this->_request->getParam("districtid");        
		$districtmodel = new District_Model_Danhmucquanhuyen();	
		$r = $districtmodel->SelectAllDistrictOfCity($cityid);
		$html = '';
		foreach($r as $row) {
			if($districtid == $row['DISTRICTID'])
				$select = 'selected';
			else
				$select = '';
			$html .= '<option value="'. $row['DISTRICTID'] .'" '. $select .'>'. $row['NAME'] .'</option>';
		}
        echo $html;
        exit;
    }
}

